package com.cos.openapi.services.impl;

import com.cos.openapi.common.ResponseResult;
import com.cos.openapi.common.ResultTable;
import com.cos.openapi.dao.AdminUserMapper;
import com.cos.openapi.entity.AdminUser;
import com.cos.openapi.services.AdminUserServices;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.sun.xml.internal.ws.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: COS
 * @time: 2021/11/23 16:45
 * @description:
 */
@Service
public class AdminUserServicesImpl implements AdminUserServices {
    @Autowired
    private AdminUserMapper userMapper;

    @Override
    public AdminUser getUserByEmail(String email) {
        if (StringUtil.isNotEmpty(email)) {
            AdminUser userByEmail = userMapper.getUserByEmail(email);
            if (userByEmail != null) {
                return userByEmail;
            }
        }
        return null;
    }

    @Override
    public AdminUser doLogin(String email, String password) {
        if (StringUtil.isNotEmpty(email) && StringUtil.isNotEmpty(password)) {
            AdminUser user = userMapper.getUserByEmail(email);
            if (user != null && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public ResultTable table(Integer page, Integer limit, String realName, String email, Integer status) {
        PageHelper.startPage(page, limit);
        List<AdminUser> users = userMapper.table(realName, email, status);
        PageInfo pi = new PageInfo(users);

        return new ResultTable(0, "msg", (int) pi.getTotal(), pi.getList());
    }

    @Override
    public List<Integer> getUserRole(Integer userId) {
        if (userId > 0) {
            return userMapper.getUserRole(userId);
        }
        return null;
    }

    @Override
    public ResponseResult assignRole(Integer userId, Integer[] roleIds) {
        //1 删除role user 中间的所有 userid的数据
        int i = userMapper.delUserRole(userId);
        //2 进行批量新增
        int i2 = userMapper.addUserRole(userId, roleIds);
        return ResponseResult.getSuccess();
    }

    @Override
    public ResponseResult delUsers(Integer[] ids) {
        int i = userMapper.delUsers(ids);
        return ResponseResult.getSuccess();
    }

    @Override
    public ResponseResult addUser(AdminUser user) {
        if (user != null) {
            int i = userMapper.insertSelective(user);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("请提供数据");
    }

    @Override
    public ResponseResult updateUser(AdminUser user) {
        if (user != null) {
            int i = userMapper.updateByPrimaryKeySelective(user);
            if (i > 0) {
                return ResponseResult.getSuccess();
            }
        }
        
        return ResponseResult.getFail("请提供数据");
    }
}
